﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using net.veekee.common;
using System.Text.RegularExpressions;
using net.veekee.common.rules;
using net.veekee.logprocessor.plugin.input;
using net.veekee.logprocessor.plugin.context;
using net.veekee.common.data;

namespace net.veekee.logprocessor.plugin.input.pack
{
    public class LogFileProcessor : AProcessor<FileEntry>
    {
        protected override bool ProcessFile(String archive, String filename)
        {
            try
            {
                Regex regex = new Regex(Options.GetInstance().Get("input.log.pattern"));
                String[] regexGroupNames = regex.GetGroupNames();
                
                int maxsize = 999999;
                Int32.TryParse(Options.GetInstance().Get("buffer.maxsize"), out maxsize);

                FileEntry entry = null;

                using (System.IO.StreamReader sr = new StreamReader(filename, Encoding.GetEncoding(Options.GetInstance().Get("input.encoding"))))
                {
                    this.stats_bytetoread = sr.BaseStream.Length;
                    while (!sr.EndOfStream)
                    {
                        this.stats_byteread = sr.BaseStream.Position;
                        String line = sr.ReadLine();
                        this.stats_read++;
                        Log.GetInstance().Trace("Line #" + this.stats_read + " selected: " + line);

                        if (line.Length > maxsize)
                        {
                            Log.GetInstance().Trace("Line #" + this.stats_read + " skipped due to exceeded length.");
                            this.stats_discarded++;
                            this.OnNewDiscardedItem(entry);
                            continue;
                        }

                        entry = new FileEntry();
                        entry.Line = this.stats_read;
                        entry.Filename = filename;
                        entry.Archive = archive;
                        entry.FullData = line;

                        Match m = regex.Match(line);
                        if (!m.Success)
                        {
                            Log.GetInstance().Trace("Line #" + this.stats_read + " skipped");
                            this.stats_discarded++;
                            this.OnNewDiscardedItem(entry);
                            continue;
                        }

                        for (int i = 0; i < m.Groups.Count; i++)
                        {
                            if (!m.Success)
                                continue;
                            entry.Fields.Set<String>(regexGroupNames[i], m.Groups[i].ToString());
                        }

                        this.OnNewItem(entry);
                    }
                }
                return true;
            }
            catch (Exception e)
            {
                Log.GetInstance().Debug(e.ToString());
            }
            finally
            {
                Log.GetInstance().WriteProgressBarEnd();
            }
            return false;
        }
    }
}
